<template>
  <ych-modal
    :fullscreen="false"
    :visible="visible"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel">

    <view-select-redirect v-if="type === 'table'" ref="container" :viewCode="viewCode" :multi="multi" :stores="stores"
                          :rows="rows" :keys="keys" @selected="handleOk" :fks="fks" :parameterValue="parameterValue" @setDialogTitle="setDialogTitle"/>
    <view-editable-select-redirect v-else-if="type === 'editableTable'" ref="editableContainer" :viewCode="viewCode" :multi="multi" :stores="stores"
                                   :rows="rows" :keys="keys" @selected="handleOk" :fks="fks" :parameterValue="parameterValue" @setDialogTitle="setDialogTitle"/>
    <tree-select-redirect v-else-if="type === 'tree'" ref="treeContainer" :viewCode="viewCode" :multi="multi" :stores="stores"
                          :rows="rows" :keys="keys" @selected="handleOk" :labelField="labelField"/>

    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{ $t('m.common.button.close') }}</a-button>
      <a-button v-if="!hideClearBut" key="handleClear" @click="handleClear">{{ $t('m.common.button.clear') }}</a-button>
      <a-button key="submit" type="primary" @click="handleOk">{{ $t('m.common.button.ok') }}</a-button>
    </template>

    <template slot="title">
      <span v-if="dialogTitle" v-html="dialogTitle"></span>
      <span v-else>{{ title }}</span>
    </template>
  </ych-modal>
</template>

<script>

  import ViewSelectRedirect from './ViewSelectRedirect'
  import ViewEditableSelectRedirect from './ViewEditableSelectRedirect'
  import TreeSelectRedirect from './TreeSelectRedirect'
  import { getAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'ViewSelectModal',
    components: {
      ViewSelectRedirect,
      ViewEditableSelectRedirect,
      TreeSelectRedirect,
    },
    props: {
      multi: Boolean,
      viewCode: String,
      rows: Array,
      keys: Array,
      labelField: String,
      fks: {
        type: Array,
        default: () => []
      },
      hideClearBut: {
        type: Boolean,
        default: false
      },
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    data () {
      return {
        title: this.$t('m.eform.formDesign.label.xuan-ze-shu-ju'),
        visible: false,
        type: null,
        url: {
          getViewType: YchConstant.PREFIX_EFORM + '/engine/viewEngine/getViewType'
        },
        parameterValue: [],
        dialogTitle: null
      }
    },
    methods: {
      show (parameterValue) {
        this.parameterValue = parameterValue || []
        this.visible = true
        this.type = null
      },
      handleOk () {
        if (this.type === 'table') {
          if (this.$refs.container.rowSelection.selectionRows.length === 0) {
            this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-xuan-ze-shu-ju'))
            return
          }
          this.$emit('ok', this.$refs.container.rowSelection.selectionRows)
          this.handleCancel()
        } else if (this.type === 'editableTable') {
          const selectionRows = this.$refs.editableContainer.getCheckboxRecords()
          if (selectionRows.length === 0) {
            this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-xuan-ze-shu-ju'))
            return
          }
          this.$emit('ok', selectionRows)
          this.handleCancel()
        } else if (this.type === 'tree') {
          if (this.$refs.treeContainer.checkedRows.length === 0) {
            this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-xuan-ze-shu-ju'))
            return
          }
          this.$emit('ok', this.$refs.treeContainer.checkedRows)
          this.handleCancel()
        }
      },
      handleClear () {
        this.$emit('ok')
        this.handleCancel()
      },
      handleCancel () {
        this.visible = false
        this.type = null
        this.dialogTitle = null
      },
      init () {
        getAction(this.url.getViewType, { viewCode: this.viewCode }).then((res) => {
          if (res.success) {
            this.type = res.result
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
        })
      },
      setDialogTitle(dialogTitle){
        this.dialogTitle = dialogTitle
      }
    },
    watch: {
      visible: {
        handler (value) {
          if (value) {
            this.init()
          }
        },
        immediate: true
      }
    }
  }
</script>
